//
//  Created by  fred on 2017/1/12.
//  Copyright © 2016年 Alibaba. All rights reserved.
//

package com.huangdada.hfweather.utils;
import com.alibaba.cloudapi.sdk.client.ApacheHttpClient;
import com.alibaba.cloudapi.sdk.enums.Scheme;
import com.alibaba.cloudapi.sdk.enums.HttpMethod;
import com.alibaba.cloudapi.sdk.model.ApiRequest;
import com.alibaba.cloudapi.sdk.model.ApiResponse;
import com.alibaba.cloudapi.sdk.model.ApiCallback;
import com.alibaba.cloudapi.sdk.model.HttpClientBuilderParams;
import com.alibaba.cloudapi.sdk.enums.ParamPosition;
import com.alibaba.cloudapi.sdk.enums.WebSocketApiType;
import com.alibaba.fastjson.JSONObject;


public class HttpApiClient extends ApacheHttpClient{
    public final static String HOST = "http://iweather.market.alicloudapi.com";
    static HttpApiClient instance = new HttpApiClient();
    public static HttpApiClient getInstance(){return instance;}

    @Override
    public void init(HttpClientBuilderParams httpClientBuilderParams){
        httpClientBuilderParams.setScheme(Scheme.HTTP);
        httpClientBuilderParams.setHost(HOST);
        super.init(httpClientBuilderParams);
    }

    /**
     * 按GPS查询天气
     * @param needday
     * @param lng
     * @param lat
     * @param from
     * @param callback
     */
    public void getWeatherByGPS(String needday , String lng , String lat , String from , ApiCallback callback) {
        String path = "/gps";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("needday" , needday , ParamPosition.QUERY , true);
        request.addParam("lng" , lng , ParamPosition.QUERY , true);
        request.addParam("lat" , lat , ParamPosition.QUERY , true);
        request.addParam("from" , from , ParamPosition.QUERY , false);



        sendAsyncRequest(request , callback);
    }

    /**
     * 按GPS查询天气SyncMode
     * @param needday
     * @param lng
     * @param lat
     * @param from
     * @return
     */
    public ApiResponse getWeatherByGPSAndSyncMode(String needday , String lng , String lat , String from) {
        String path = "/gps";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("needday" , needday , ParamPosition.QUERY , true);
        request.addParam("lng" , lng , ParamPosition.QUERY , true);
        request.addParam("lat" , lat , ParamPosition.QUERY , true);
        request.addParam("from" , from , ParamPosition.QUERY , false);



        return sendSyncRequest(request);
    }

    /**
     * 按IP地址查询天气
     * @param needday
     * @param ip
     * @param callback
     */
    public void getWeatherByIpAddr(String needday , String ip , ApiCallback callback) {
        String path = "/ipquery";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("needday" , needday , ParamPosition.QUERY , true);
        request.addParam("ip" , ip , ParamPosition.QUERY , true);



        sendAsyncRequest(request , callback);
    }

    /**
     * 按IP地址查询天气SyncMode
     * @param needday
     * @param ip
     * @return
     */
    public ApiResponse getWeatherByIpAddrAndSyncMode(String needday , String ip) {
        String path = "/ipquery";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("needday" , needday , ParamPosition.QUERY , true);
        request.addParam("ip" , ip , ParamPosition.QUERY , true);



        return sendSyncRequest(request);
    }

    /**
     * 按景区景点查询天气
     * @param jq
     * @param city
     * @param needday
     * @param callback
     */
    public void getWeatherByAttractions(String jq , String city , String needday , ApiCallback callback) {
        String path = "/scenic";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("jq" , jq , ParamPosition.QUERY , true);
        request.addParam("city" , city , ParamPosition.QUERY , false);
        request.addParam("needday" , needday , ParamPosition.QUERY , true);



        sendAsyncRequest(request , callback);
    }

    /**
     * 按景区景点查询天气SyncMode
     * @param jq
     * @param city
     * @param needday
     * @return
     */
    public ApiResponse getWeatherByAttractionsAndSyncMode(String jq , String city , String needday) {
        String path = "/scenic";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("jq" , jq , ParamPosition.QUERY , true);
        request.addParam("city" , city , ParamPosition.QUERY , false);
        request.addParam("needday" , needday , ParamPosition.QUERY , true);



        return sendSyncRequest(request);
    }

    /**
     * 按省市区查询天气
     * @param needday
     * @param prov
     * @param city
     * @param area
     * @param callback
     */
    public void getWeatherByProvinces(String needday , String prov , String city , String area , ApiCallback callback) {
        String path = "/address";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("needday" , needday , ParamPosition.QUERY , true);
        request.addParam("prov" , prov , ParamPosition.QUERY , true);
        request.addParam("city" , city , ParamPosition.QUERY , true);
        request.addParam("area" , area , ParamPosition.QUERY , false);



        sendAsyncRequest(request , callback);
    }

    /**
     * 按省市区查询天气SyncMode
     * @param needday
     * @param prov
     * @param city
     * @param area
     * @return
     */
    public ApiResponse getWeatherByProvincesAndSyncMode(String needday , String prov , String city , String area) {
        String path = "/address";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("needday" , needday , ParamPosition.QUERY , true);
        request.addParam("prov" , prov , ParamPosition.QUERY , true);
        request.addParam("city" , city , ParamPosition.QUERY , true);
        request.addParam("area" , area , ParamPosition.QUERY , false);



        return sendSyncRequest(request);
    }


    /**
     * 查询历史天气数据
     * @param prov
     * @param city
     * @param area
     * @param date
     * @param callback
     */
    public void getWeatherByHistory(String prov , String city , String area , String date , ApiCallback callback) {
        String path = "/history";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("prov" , prov , ParamPosition.QUERY , true);
        request.addParam("city" , city , ParamPosition.QUERY , true);
        request.addParam("area" , area , ParamPosition.QUERY , false);
        request.addParam("date" , date , ParamPosition.QUERY , true);



        sendAsyncRequest(request , callback);
    }

    /**
     * 查询历史天气数据SyncMode
     * @param prov
     * @param city
     * @param area
     * @param date
     * @return
     */
    public ApiResponse getWeatherByHistoryAndSyncMode(String prov , String city , String area , String date) {
        String path = "/history";
        ApiRequest request = new ApiRequest(HttpMethod.GET , path);
        request.addParam("prov" , prov , ParamPosition.QUERY , true);
        request.addParam("city" , city , ParamPosition.QUERY , true);
        request.addParam("area" , area , ParamPosition.QUERY , false);
        request.addParam("date" , date , ParamPosition.QUERY , true);



        return sendSyncRequest(request);
    }

}